var express = require('express')
var User = require('./models/user')
var Topic = require('./models/topic')
var md5 = require('blueimp-md5')

var router = express.Router()

router.get('/',function(req,res){
	Topic.find((err,data) => {
		if(err) {
			return res.end('Server Error')
		}
		let PageCode = []
		let length = Math.ceil(data.length / 6)
		for(var i = 1;i <= length; i++)
		PageCode.push(i)
		data = data.slice(0,6)
		// console.log( req.session.user )
		res.render('index.html',{
				user: req.session.user,
				topic: data,pageCode:PageCode
		})
	})
	
})

router.get('/login',function(req,res){
	res.render('login.html')
})

/* router.post('/login',function(req,res){
	var body = req.body
	User.findOne({
		email : body.email,
		password : md5(md5(body.password))
	},function(err , user){
		if(err){
			return res.status(500).json({
				err_code: 500,
				message: err.message
			})
		}
		
		if(!user) {
			return res.status(200).json({
				err_code: 1,
				message: 'email or password error'
			})
		}
		
		req.session.user = user
		res.status(200).json({
			err_code: 0,
			message: 'OK'
		})
	})
}) */
router.post('/login',function(req,res,next){
	var body = req.body
	User.findOne({
		email : body.email,
		password : md5(md5(body.password))
	},function(err , user){
		if(err){
			/* return res.status(500).json({
				err_code: 500,
				message: err.message
				
			}) */
			return next(err)
		}
		
		if(!user) {
			return res.status(200).json({
				err_code: 1,
				message: 'email or password error'
			})
		}
		
		req.session.user = user
		res.status(200).json({
			err_code: 0,
			message: 'OK'
		})
	})
})

router.get('/topics/show',(req,res) => {
	Topic.findOne({_id:req.query.id},(err,data) => {
		if(err) {
			throw(err);
		}
		res.render('topic/show.html',{
			topic:data,
			user: req.session.user
			})
	})
})

router.get('/page/choose',(req,res) => {
	const num = parseInt(req.query.num,10);
	console.log(num);
	const startNum = (num - 1) * 6;
	const endNum = startNum + 6;
	Topic.find((err,data) => {
		if(err) {
			return res.end('Server Error');
		}
		data = data.slice(startNum,endNum);
		res.json({topic:data});
	})
})


router.get('/topics/new',(req,res) => {
	res.render('topic/new.html',{user:req.session.user});
})

router.post('/topics/new',(req,res) => {
	var body = req.body;
	new Topic(body).save((err,data) => {
		if(err) {
			return res.end('Server Error');
		}
		res.redirect('/');
	})
})

router.get('/settings/profile',function(req,res) {

	res.render('settings/profile.html',{user:req.session.user});
})

router.post('/settings/profile',function(req,res) {
	var body = req.body;
	body.birthday = body.birthday + '';
	User.findByIdAndUpdate(req.session.user._id,body,function(err,data) {
		if(err) {
			return res.json({
				err_code:500
			})
		}
		req.session.user = data;
		console.log(req.session.user);
		res.json({
			err_code:0
		})
	})
})

router.get('/logout',function(req , res){
	req.session.user = null
	res.redirect('/login')
})

router.get('/register',function(req,res){
	res.render('register.html')
})

router.post('/register',function(req,res){
	var body = req.body
	User.findOne({
		$or: [
			{
				email: body.email
			},
			{
				nickname: body.nickname
			}
		]
	},function(err,data){
		if(err){
			return res.status(500).json({
				error: 500,
				message: '服务端错误'
			})
		}
		if(data){
			return res.status(200).json({
				err_code: 1,
				message: 'Email or nickname aleady.'
			})
		}
		body.password = md5(md5(body.password))
		new User(body).save(function(err,user){
			if(err){
				return res.status(500).json({
					err_code: 500,
					message: 'Internal error'
				})
			}
			//注册成功，使用session 记录用户登录状态
			req.session.user = user
			// console.log('ok')
			res.status(200).json({
				err_code: 0,
				message: 'OK'
			})
		})
		
		
	})
})

module.exports = router
